package com.appbee.core.client.upload;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.xhr.client.ReadyStateChangeHandler;

public class XMLHttpRequest extends JavaScriptObject
{
	protected XMLHttpRequest()
	{
	}

	/**
	 * Creates an XMLHttpRequest object.
	 * 
	 * @return the created object
	 */
	public static native XMLHttpRequest create() /*-{
		// Don't check window.XMLHttpRequest, because it can
		// cause cross-site problems on IE8 if window's URL
		// is javascript:'' .
		if ($wnd.XMLHttpRequest) {
			return new $wnd.XMLHttpRequest();
		} else {
			try {
				return new $wnd.ActiveXObject('MSXML2.XMLHTTP.3.0');
			} catch (e) {
				return new $wnd.ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}-*/;

	/**
	 * Gets the response text.
	 * <p>
	 * See <a href="http://www.w3.org/TR/XMLHttpRequest/#responsetext"
	 * >http://www.w3.org/TR/XMLHttpRequest/#responsetext</a>.
	 * 
	 * @return the response text
	 */
	public final native String getResponseText() /*-{
		return this.responseText;
	}-*/;

	/**
	 * Gets the status code.
	 * <p>
	 * See <a href="http://www.w3.org/TR/XMLHttpRequest/#status"
	 * >http://www.w3.org/TR/XMLHttpRequest/#status</a>.
	 * 
	 * @return the status code
	 */
	public final native int getStatus() /*-{
		return this.status;
	}-*/;

	/**
	   * Gets the status text.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#statustext"
	   * >http://www.w3.org/TR/XMLHttpRequest/#statustext</a>.
	   * 
	   * @return the status text
	   */
	  public final native String getStatusText() /*-{
	    return this.statusText;
	  }-*/;
	  
	  public final native void overrideMimeType(String mimeType) /*-{
	    this.overrideMimeType(mimeType);
	  }-*/;
	  
	  /**
	   * Opens an asynchronous connection.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#open"
	   * >http://www.w3.org/TR/XMLHttpRequest/#open</a>.
	   * 
	   * @param httpMethod the HTTP method to use
	   * @param url the URL to be opened
	   */
	  public final native void open(String httpMethod, String url) /*-{
	    this.open(httpMethod, url, true);
	  }-*/;

	  /**
	   * Opens an asynchronous connection.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#open"
	   * >http://www.w3.org/TR/XMLHttpRequest/#open</a>.
	   * 
	   * @param httpMethod the HTTP method to use
	   * @param url the URL to be opened
	   * @param user user to use in the URL
	   */
	  public final native void open(String httpMethod, String url, String user) /*-{
	    this.open(httpMethod, url, true, user);
	  }-*/;

	  /**
	   * Opens an asynchronous connection.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#open"
	   * >http://www.w3.org/TR/XMLHttpRequest/#open</a>.
	   * 
	   * @param httpMethod the HTTP method to use
	   * @param url the URL to be opened
	   * @param user user to use in the URL
	   * @param password password to use in the URL
	   */
	  public final native void open(String httpMethod, String url, String user,
	      String password) /*-{
	    this.open(httpMethod, url, true, user, password);
	  }-*/;
	  
	  /**
	   * Sets the {@link ReadyStateChangeHandler} to be notified when the object's
	   * ready-state changes.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#onreadystatechange"
	   * >http://www.w3.org/TR/XMLHttpRequest/#onreadystatechange</a>.
	   * 
	   * <p>
	   * Note: Applications <em>must</em> call {@link #clearOnReadyStateChange()}
	   * when they no longer need this object, to ensure that it is cleaned up
	   * properly. Failure to do so will result in memory leaks on some browsers.
	   * </p>
	   * 
	   * @param handler the handler to be called when the ready state changes
	   * @see #clearOnReadyStateChange()
	   */
	  public final native void setOnReadyStateChange(ReadyStateChangeHandler handler) /*-{
	    // The 'this' context is always supposed to point to the xhr object in the
	    // onreadystatechange handler, but we reference it via closure to be extra sure.
	    var _this = this;
	    this.onreadystatechange = $entry(function() {
	      handler.@com.google.gwt.xhr.client.ReadyStateChangeHandler::onReadyStateChange(Lcom/google/gwt/xhr/client/XMLHttpRequest;)(_this);
	    });
	  }-*/;

	  /**
	   * Sets a request header.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#setrequestheader"
	   * >http://www.w3.org/TR/XMLHttpRequest/#setrequestheader</a>.
	   * 
	   * @param header the header to be set
	   * @param value the header's value
	   */
	  public final native void setRequestHeader(String header, String value) /*-{
	    this.setRequestHeader(header, value);
	  }-*/;
	  
	  public final native XMLHttpRequestUpload getUpload() /*-{
	  	return this.upload;
	  }-*/;
	  
	  public final native void sendAsBinary(String datastr) /*-{
	  	if(this.sendAsBinary) this.sendAsBinary(datastr);
	  	else
	  	{
	  		var data = new ArrayBuffer(datastr.length);
	        var ui8a = new Uint8Array(data, 0);
	        for (var i=0; i<datastr.length; i++) {
	                ui8a[i] = (datastr.charCodeAt(i) & 0xff);
	        }
	        this.send(data);
	  	}
	  }-*/;
	  
	  /**
	   * Initiates a request with data.  If there is no data, specify null.
	   * <p>
	   * See <a href="http://www.w3.org/TR/XMLHttpRequest/#send"
	   * >http://www.w3.org/TR/XMLHttpRequest/#send</a>.
	   * 
	   * @param requestData the data to be sent with the request
	   */
	  public final native void send(String requestData) /*-{
	    this.send(requestData);
	  }-*/;

}
